package com.tx.activemq._01_c_p._02_topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * activemq生产者-主题
 */
public class TopicProducer {

    private static final String DEFAULT_URL = "tcp://192.168.254.30:61616";

    private static final String TOPIC_NAME = "mercury_topic";

    public static void main(String[] args) throws JMSException {
        //1. activemqConnectionFactory
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(DEFAULT_URL);

        //2. activemqConnection
        Connection connection = factory.createConnection();

        //3. 连接
        connection.start();

        //4. session
        Session session  = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5. 创建topic-目的地
        Topic topic = session.createTopic(TOPIC_NAME);

        //6.  topicProducer,并发送
        MessageProducer producer = session.createProducer(topic);
        for (int i = 1; i <= 3; i++) {
            TextMessage msg = session.createTextMessage("msg" + i);
            producer.send(msg);
        }

        //7. 关闭资源
        producer.close();
        session.close();
        connection.close();
    }
}